var voltage_type=null;
var range_type=null;
$(function () {
    $.when(getdatadictionary("range_type"),
        getdatadictionary("voltage_type")
    ).done(
        $("#jqGrid").jqGrid({

            url: baseURL + 'eys/eprange/list?state=isdelno&pGuid='+ GetUrlParam("guid"),
            datatype: "json",
            colModel: [
                { label: 'guid', name: 'guid', index: 'guid', width: 50, key: true,hidden: true  },
                { label: '所属项目ID', name: 'pGuid', index: 'p_guid', width: 80,hidden: true  },
                { label: '类型', name: 'pRType', index: 'p_r_type', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in range_type) {
                            if (cellvalue == range_type[d].dKey) return range_type[d].dValue;
                        }
                    }
                },
                { label: '电压等级', name: 'pRVoltage', index: 'p_r_voltage', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in voltage_type) {
                            if (cellvalue == voltage_type[d].dKey) return voltage_type[d].dValue;
                        }
                    }
                },
                { label: '名称', name: 'pRName', index: 'p_r_name', width: 80 },
                { label: '数据状态', name: 'pRDatastate', index: 'p_r_datastate', width: 80,hidden: true  }
            ],
            viewrecords: true,
            height: 385,
            rowNum: 10,
            rowList : [10,30,50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth:true,
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader : {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames : {
                page:"page",
                rows:"limit",
                order: "order"
            },
            gridComplete:function(){
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            }
        })
    )
});
function getdatadictionary(sss) {

    $.get(baseURL + "api/bdatadictionary/select?d_code=" + sss, function (r) {

        if (sss == "voltage_type") {
            voltage_type = r.returnlist;
        }
        if (sss == "range_type") {
            range_type = r.returnlist;
        }
        vm.range_type =range_type;
        vm.voltage_type =voltage_type;
    });
}
var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        range_type: null,
        voltage_type: null,
        ePRange: {pGuid:GetUrlParam("guid")}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.range_type = range_type;
            vm.voltage_type =voltage_type;
            vm.ePRange = {pGuid:GetUrlParam("guid")};
        },
        update: function (event) {
            var guid = getSelectedRow();
            if(guid == null){
                return ;
            }
            vm.range_type = range_type;
            vm.voltage_type =voltage_type;
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(guid)
        },
        saveOrUpdate: function (event) {
            var url = vm.ePRange.guid == null ? "eys/eprange/save" : "eys/eprange/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.ePRange),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if(guids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "eys/eprange/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(guid){
            $.get(baseURL + "eys/eprange/info/"+guid, function(r){
                vm.ePRange = r.ePRange;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        }
    }
});